package com.ycode.bang.anbang.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ycode.bang.anbang.MyApp;
import com.ycode.bang.anbang.R;
import com.ycode.bang.anbang.activity.RepairDetailActivity;
import com.ycode.bang.anbang.activity.RepairingDetailActivity;
import com.ycode.bang.anbang.model.Order;

import java.util.List;


/**
 * ken
 * Description :
 */
public class EventAdapter extends RecyclerView.Adapter<EventAdapter.ViewHolder> {
    public Context mContext;
    public List<Order> mBean;
    public int state = 0;//维修状态：1，新接收；2，进行中；3已处理；4.已完成

    public EventAdapter(Context mContext, List<Order> mBean, int state) {
        this.mContext = mContext;
        this.mBean = mBean;
        this.state = state;
    }


    @Override
    public EventAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_event, parent, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        if (state == 1 || !MyApp.instance.isManage()) {
            holder.lin_name.setVisibility(View.GONE);
        } else if (state != 1) {
            holder.lin_name.setVisibility(View.VISIBLE);
        }
        final Order bill = mBean.get(position);
//        holder.tvt_event_title.setText(bill.name);
//        holder.tvt_importance.setText(bill.time);
//        holder.tvt_time.setText(bill.amount);
//        holder.tvt_address.setText(bill.amount);
//        holder.tvt_name_phone.setText(bill.amount);
        if (!TextUtils.isEmpty(bill.getProject())) {
            holder.tvt_event_title.setText(bill.getProject());
        } else {
            holder.tvt_event_title.setText(bill.getType());
        }
//        holder.tvt_importance.setText(bill.);
        holder.tvt_time.setText(bill.getCreateTime());
        holder.tvt_address.setText(bill.getAddress());
        holder.tvt_name_phone.setText(bill.getApplicationName() + "     " + bill.getPhoneNumber());
        if (!TextUtils.isEmpty(bill.getWorkName())) {
            holder.tv_name.setText(bill.getWorkName());
        }
        if (!TextUtils.isEmpty(bill.getLevel())) {
            holder.tvt_importance.setText(bill.getLevel());
            if (bill.getLevel().equals("一般")) {
                holder.tvt_importance.setTextColor(mContext.getResources().getColor(R.color.green));
            } else {
                holder.tvt_importance.setTextColor(mContext.getResources().getColor(R.color.red));
            }
        }
        if (state == 2 && MyApp.instance.isManage()) {
            SharedPreferences mPreference = mContext.getSharedPreferences("huancun", mContext.MODE_PRIVATE);
            SharedPreferences.Editor edit = mContext.getSharedPreferences("huancun", mContext.MODE_PRIVATE).edit();
            if (!TextUtils.isEmpty(mPreference.getString(bill.getId() + "", ""))) {
                holder.lin_fail.setVisibility(View.VISIBLE);
            } else {
                holder.lin_fail.setVisibility(View.GONE);
            }
        }
        holder.lin_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (state == 1) {
                    Intent intent = new Intent(mContext, RepairDetailActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putInt("id", bill.getId());
                    bundle.putInt("tag", 1);
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);
                } else if (state == 2) {
                    Intent intent = new Intent(mContext, RepairingDetailActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putInt("state", 2);
                    bundle.putInt("id", bill.getId());
                    bundle.putInt("tag", 1);
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);
                } else if (state == 3) {
                    Intent intent = new Intent(mContext, RepairingDetailActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putInt("state", 3);
                    bundle.putInt("tag", 1);
                    bundle.putInt("id", bill.getId());
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);
                } else if (state == 4) {
                    Intent intent = new Intent(mContext, RepairingDetailActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putInt("state", 4);
                    bundle.putInt("tag", 1);
                    bundle.putInt("id", bill.getId());
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);
                }

            }
        });
    }

    @Override
    public int getItemCount() {
        return mBean.size();
//        return 10;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView tvt_event_title;
        public TextView tvt_importance;
        public TextView tvt_time;
        public TextView tvt_address;
        public TextView tvt_name_phone;
        public LinearLayout lin_item;//布局，用于点击事件
        public LinearLayout lin_name;//布局，分配的人员显示
        public TextView tv_name;//分配的人的名字
        public LinearLayout lin_fail;//布局，上传失败显示的

        public ViewHolder(View v) {
            super(v);
            tvt_event_title = (TextView) v.findViewById(R.id.tvt_event_title);
            tvt_importance = (TextView) v.findViewById(R.id.tvt_importance);
            tvt_time = (TextView) v.findViewById(R.id.tvt_time);
            tvt_address = (TextView) v.findViewById(R.id.tvt_address);
            tvt_name_phone = (TextView) v.findViewById(R.id.tvt_name_phone);
            tv_name = (TextView) v.findViewById(R.id.tv_name);
            lin_item = (LinearLayout) v.findViewById(R.id.lin_item);
            lin_name = (LinearLayout) v.findViewById(R.id.lin_name);
            lin_fail = (LinearLayout) v.findViewById(R.id.lin_fail);
        }
    }
}
